/**
 * 
 */
package com.magnusart.scrumppm.presenters;

import static com.magnusart.scrumppm.model.States.Transition.CHOOSER_TO_DETAILS;
import static com.magnusart.scrumppm.model.States.Transition.CHOOSER_TO_NEW;

import java.util.Collections;
import java.util.Observable;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.List;

import com.magnusart.scrumify.dao.CommonDAO;
import com.magnusart.scrumppm.gui.ProjectChooser;
import com.magnusart.scrumppm.model.Project;
import com.magnusart.scrumppm.model.ProjectBuilder;
import com.magnusart.scrumppm.model.States.Transition;
import com.magnusart.scrumppm.presenters.GlobalEvents.Events;

/**
 * This class handles retrieveing data from the database and displaying/updating it in the GUI.
 * @author Magnus Andersson
 *
 */
public class ProjectChooserPresenter extends AbstractPresenter<ProjectChooser, CommonDAO<Project, Project, ProjectBuilder>>
{

	private java.util.List<Project> projects;
	private String selectedProject;
	private boolean allowedToSubmit = false;
	private boolean searchCleared = false;
	
	/**
	 * Takes the combo box that is supposed to be updated.
	 * @param combo The combo box that is supposed to be updated.
	 * @param projectDAO The DAO to use when accessing the datastore.
	 */

	public ProjectChooserPresenter( ProjectChooser view, CommonDAO<Project, Project, ProjectBuilder> projectDAO ) {
		super( view, projectDAO );

		addListeners();
		initView();
	}

	@Override
	protected void addListeners() {
		view.getOpenProject().addListener( SWT.Selection, this );
		view.getProjectList().addListener( SWT.Selection, this );
		view.getSearch().addListener( SWT.FocusIn, this );
	}

	/**
	 * Initializes the view
	 */
	@Override
	protected void initView( ) {
		view.getProjectList().setItems( updateProjectsList() );
		view.getOpenProject().setEnabled( allowedToSubmit );
		view.layout(); // To make changes persistent.
	}


	/**
	 * Retrieves a list of project names from the database.
	 * @return List of project names.
	 */
	private String[] updateProjectsList() {

		projects = dao.getAll( null );
		String[] names = new String[projects.size()];
		
		for( int i = 0; i < projects.size(); i++ ) {
			names[i] = projects.get(i).getName();
		}

		return names;
	}

	/*
	 * Do not forget to add an event listener for each case!
	 */
	@Override
	public void handleEvent( Event e ) {
		if( e.widget.getData() instanceof WidgetTypes ) {
			WidgetTypes s = (WidgetTypes)( e.widget.getData() );
			switch( s) {
			case OpenProjectButton :
				doDisplay();
				break;
			case CreateNewButton :
				doCreate();
				break;
			case SelectProjectInList:
				doSelectList( (List)e.widget );
				break;
			case ProjectSearch:
				doClearSearchText();
				break;
			}
		}
	}
	
	private void doSelectList( List l ) {
		String[] selected = l.getSelection();
		if( selected.length == 1 ) {
			selectedProject = selected[0];
			allowedToSubmit = true;
			view.getOpenProject().setEnabled( allowedToSubmit );
		}
	}

	private void doCreate() {
		Transition.to( CHOOSER_TO_NEW );
	}

	private void doDisplay() {
		ProjectBuilder builder = new ProjectBuilder();
		builder.setName( selectedProject );
		
		int index = Collections.binarySearch( projects, new Project( builder ) );
		GlobalEvents.setCurrentProject( projects.get( index ) );
		Transition.to( CHOOSER_TO_DETAILS );
	}
	
	private void doClearSearchText() {
		if( !searchCleared ) {
				view.getSearch().setText( "" );
				view.getSearch().setForeground( new Color( Display.getCurrent(), new RGB( 0, 0, 0) ) );
				searchCleared = true;
		}
	}

	@Override
	public void update( Observable obs, Object obj ) {
		if( obs.equals(  GlobalEvents.observe() ) && obj instanceof GlobalEvents.Events) {
			Events change = (Events) obj;
			switch( change ) {
			case REFRESH_PROJECT_LISTINGS:
				initView();
				break;
			}
		}
	}
}
